package com.one.common.view.pagestate.statepage;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.one.common.R;
import com.one.common.view.pagestate.page.PageInterFace;


/**
 * creator: zhulunjun
 * time:    2018/4/10
 * describe: 页面状态的处理
 * 为了让fragment和activity使用一套逻辑，抽离一个类来处理公共逻辑
 */

public class StatePageHandle implements StatePageInterface {

    FrameLayout flContent;
    private View viewError;
    private View viewEmpty;
    private View viewLoading;

    private TextView textEmpty, textError;
    private ImageView imageEmpty, imageError;
    private LinearLayout llEmpty, llError, llLoading;

    private LoadingStateDelegate loadingStateDelegate;
    public View successView;
    private PageInterFace mPage;
    private Context mContext;
    private StatePageInterface mStatePage;

    private String emptyStr = "";
    private int emptyRes = -1;

    public StatePageHandle(Context mContext, PageInterFace mPage, StatePageInterface mStatePage) {
        this.mPage = mPage;
        this.mStatePage =mStatePage;
        this.mContext = mContext;
    }

    /**
     * 初始化控件
     */
    public void initView(FrameLayout flContent, View viewError, View viewEmpty, View viewLoading){
        this.flContent = flContent;
        this.viewError = viewError;
        this.viewEmpty = viewEmpty;
        this.viewLoading = viewLoading;
        // 添加子类的视图
        LayoutInflater inflater =  LayoutInflater.from(mContext);
        successView = inflater.inflate(mPage.getLayoutResId(), flContent, true);
        loadingStateDelegate = new LoadingStateDelegate(successView, viewLoading, viewError, viewEmpty);
    }

    @Override
    public void success() {
        loadingStateDelegate.setLoadingState(LoadingStateDelegate.STATE.SUCCEED);
    }

    public void setEmptyStr(String emptyStr) {
        this.emptyStr = emptyStr;
    }

    public void setEmptyRes(int emptyRes) {
        this.emptyRes = emptyRes;
    }

    @Override
    public void empty() {
        empty(-1, null);
    }

    @Override
    public void empty(String emptyStr) {
        empty(-1, emptyStr);
    }

    @Override
    public void empty(int imageRes, String emptyStr) {

        if(imageEmpty == null || textEmpty == null){
            getEmptyView();
        }
        // 图片
        if(imageRes != -1){
            imageEmpty.setImageResource(imageRes);
        }
        // 文本
        if (!TextUtils.isEmpty(emptyStr)){
            textEmpty.setText(emptyStr);
        }
        
        loadingStateDelegate.setLoadingState(LoadingStateDelegate.STATE.EMPTY);

    }

    @Override
    public void setEmptyView(View view) {
        if(llEmpty == null){
            getEmptyView();
        }
        if(view == null)return;
        llEmpty.removeAllViews();
        llEmpty.addView(view);
    }

    @Override
    public void error() {
        error(emptyRes, emptyStr);
    }

    @Override
    public void error(String errorStr) {
        error(-1, errorStr);
    }

    @Override
    public void error(int imageRes, String errorStr) {
        loadingStateDelegate.setLoadingState(LoadingStateDelegate.STATE.ERROR);
        if(imageError == null || textError == null){
            getErrorView();
        }
        // 图片
        if(imageRes != -1){
            imageError.setImageResource(imageRes);
        }
        // 文本
        if (!TextUtils.isEmpty(errorStr)){
            textError.setText(errorStr);
        }

    }

    @Override
    public void setErrorView(View view) {
        if(llError == null){
            getErrorView();
        }
        if(view == null)return;
        llError.removeAllViews();
        llError.addView(view);
    }

    @Override
    public void loading() {
        loadingStateDelegate.setLoadingState(LoadingStateDelegate.STATE.LOADING);
    }

    @Override
    public void setLoadingView(View view) {
        if(llLoading == null){
            getLoadingView();
        }
        if(view == null)return;
        llLoading.removeAllViews();
        llLoading.addView(view);
    }

    @Override
    public void onEmptyClick() {
        mStatePage.onEmptyClick();
    }

    @Override
    public void onErrorClick() {
       mStatePage.onErrorClick();
    }

    @Override
    public boolean onEmpty() {
        return mStatePage.onEmpty();
    }

    @Override
    public boolean onError() {
        return mStatePage.onError();
    }

    private void getEmptyView(){
        llEmpty = (LinearLayout) viewEmpty;
        textEmpty = (TextView) viewEmpty.findViewById(R.id.tv_empty);
        imageEmpty = (ImageView) viewEmpty.findViewById(R.id.iv_empty);

        if(llEmpty != null) {
            llEmpty.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onEmptyClick();
                }
            });
        }
    }

    private void getErrorView(){
        llError = (LinearLayout) viewError;
        textError = (TextView) viewError.findViewById(R.id.tv_error);
        imageError = (ImageView) viewError.findViewById(R.id.iv_error);

        TextView btn = viewError.findViewById(R.id.tv_btn_error);

        if(llError != null) {
            btn.setOnClickListener(view -> onErrorClick());
        }
    }

    private void getLoadingView(){
        llLoading = (LinearLayout) viewLoading.findViewById(R.id.ll_loading);
    }




    @Override
    public int getLayoutResId() {
        return 0;
    }

    @Override
    public int getSecondLayoutResId() {
        return 0;
    }

    @Override
    public void beforeCreateView() {

    }

    @Override
    public void afterCreateView() {

    }

    @Override
    public boolean haveRootView() {
        return false;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void finishPage() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void cancelLoading() {

    }
}